package com.ming.luckyDemo.config;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;


public class HttpClientUtil {
//    public static void main(String[] args) throws InterruptedException {
//        HttpClientUtil httpClientUtil = new HttpClientUtil();
//        for (int i = 200; i < 1700; i++) {
//            httpClientUtil.aa(i);
//              Thread.sleep(1000*3);
//        }
//    }
    public void aa(int number) {
        PrintWriter out = null;
        try{
            //定义访问路径和参数
            String path = "http://localhost:9000/queryLucky";
            String param = "numbers="+number;
            URL url = new URL(path);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");

            con.setRequestProperty(
                    "User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36");
            //携带cookie
            con.setRequestProperty("Cookie", "123=c2d6f5fbe4524636a73b04fb8a9ac760795");
            // 发送POST请求必须设置如下两行
            con.setDoOutput(true);
            con.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(con.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            con.connect();

            if (con.getResponseCode() == 200) {

                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String list;
                StringBuilder sb = new StringBuilder();
                while ((list = br.readLine()) != null) {// 循环读取流
                    sb.append(list);
                }
                br.close();// 关闭流
                System.out.println(sb);
            }else{
                System.out.println("请求失败："+con.getResponseCode());
            }
            con.disconnect();// 断开连接
        }catch (Exception e){

            System.out.println(e);
        }
    }

}
